package com.mocai.merchant.spring.aspect;

import com.mocai.merchant.exception.ErrorConstant;
import com.mocai.merchant.exception.ServiceException;
import org.aspectj.lang.JoinPoint;

/**
 * 持久层异常处理切面
 */
public class ServiceExceptionAspect extends AspectTemplate {

    /**
     * 将业务层抛出的所有异常，都包装为com.mocai.merchant.exception.ServiceException进行抛出
     *
     * @param joinPoint
     * @param ex
     * @see com.mocai.merchant.exception.ServiceException
     */
    @Override
    protected void afterThrowingAdvice(JoinPoint joinPoint, Throwable ex) {
        if (ex instanceof ServiceException) {
            throw (ServiceException) ex;
        }
        ErrorConstant errorConstant = ErrorConstant.SYSTEM_001;
        throw new ServiceException(ex, errorConstant.getCode(), errorConstant.getDescription());
    }
}
